<template>
  <div class="app-container">
    <div class="information ">
      <div>班级信息</div>
      <span class="numInfo">
        班级总人数：
        <el-input
          v-model="classNum.sum"
          placeholder="请填写"
          style="width: 140px"
        />
      </span>
      <span class="numInfo">
        出席人数：
        <el-input
          v-model="classNum.present"
          placeholder="请填写"
          style="width: 140px"
          @change="setAbsent"
        />
      </span>
      <span class="numInfo">
        缺席人数：
        <el-input
          v-model="classNum.absent"
          placeholder="请填写"
          style="width: 140px"
        />
      </span>
    </div>
    <el-divider />
    <div class="table-header">民主评测表</div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column
        align="center"
        type="index"
        label="序号"
        width="60"
      />
      <el-table-column
        prop="id"
        label="学号"
        width="200"
      />
      <el-table-column
        prop="name"
        label="姓名"
        width="200"
      />
      <el-table-column
        prop="pass"
        label="同意"
      >
        <template slot-scope="scope">
          <el-input
            v-model="tableData[scope.$index].pass"
            placeholder="请输入"
            @change="setDeny(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="deny"
        label="不同意"
      >
        <template slot-scope="scope">
          <el-input v-model="tableData[scope.$index].deny" placeholder="请输入" />
        </template>
      </el-table-column>
      <el-table-column
        label="弃权"
        prop="absent"
        min-width="20"
        align="center"
      />
    </el-table>
    <div style="text-align: right; margin-top: 20px">
      <el-button size="big" type="success" @click="test">提交</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      params: {},
      classNum: {
        sum: null,
        present: null,
        absent: null
      },
      tableData: []
    }
  },
  mounted() {
  },
  activated() {
    console.log('ac')
    console.log(this.$route.query)
    this.params = this.$route.query
    this.fetchStuInfo()
  },
  deactivated() {
    console.log('de')
    this.$router.push({ query: {}})
    const path = this.$route.path
    this.$router.push(path)
  },
  methods: {
    setAbsent() {
      this.classNum.absent = this.classNum.sum - this.classNum.present
      for (let i = 0; i < this.tableData.length; i++) {
        this.tableData[i].absent = this.classNum.absent
      }
    },
    setDeny(row) {
      console.log(row)
      row.deny = this.classNum.sum - this.classNum.absent - row.pass
    },
    // 获取报名奖项的学生信息
    fetchStuInfo(params = {}) {
      this.tableData = []
      console.log('fetchStuInfo')
      params = this.params
      this.$get('price/price/teacherGetPriceInformationByClass', {
        ...params
      }).then((r) => {
        const data = r.data.data
        console.log(data)
        // for (let i = 0; i < data.length; i++) {
        for (let i = 0; i < 3; i++) {
          this.tableData.push(
            {
              pseIndex: data[i].priceStudentEnroll.pseIndex,
              id: data[i].studentInfo.siNumber,
              name: data[i].studentInfo.siName,
              pass: null,
              deny: null,
              absent: null
            }
          )
        }
        console.log(this.tableData)
      })
    },
    // 更新一个学生的民主测评结果
    uploadResults(i, params = {}) {
      console.log('UploadResults')
      console.log(i)
      params = {
        pseIndex: this.tableData[i].pseIndex,
        allNum: this.classNum.sum,
        passNum: this.tableData[i].pass,
        denyNum: this.tableData[i].deny,
        giveUpNum: this.classNum.absent
      }
      console.log(params)
      this.$get('price/price/updateDemocraticEvaluation', {
        ...params
      }).then((r) => {
        console.log(r)
      })
    },
    test() {
      console.log('Click Upload!')
      for (let i = 0; i < this.tableData.length; i++) {
        // eslint-disable-next-line no-unused-vars
        let params = {}
        this.uploadResults(i, params = {})
      }
      this.$message({
        message: '恭喜你，成功提交民主测评表！',
        type: 'success'
      })
    }
  }
}
</script>

<style>
.information {
  height: 40px;
  line-height: 40px;
  font-size: 20px;
  font-weight: bolder;
  color: #d7690f;
  background-color: white;
  display: inline-block;
}
.numInfo{
  font-weight: lighter;
  color: #312d2b;
  font-size: 16px;
}
.numInfo:nth-child(n+2){
  margin: 0 10px;
}

.table-header{
  font-size: 20px;
  color: #5daf34;
  font-weight: bolder;
  margin-bottom: 10px;
  margin-top: -10px
}
</style>

